import request from 'umi-request'
import { Button } from 'antd'
import { ProTable } from '@ant-design/pro-components'

export const TableModal = ({
    headers,
    actionRef,
    CurrentData,
    data,
    paramsData,
    setParamsData,
    Scroll,
    DefaultData,
}) => {
    return (
        <div className="ProTable">
            <ProTable
                actionRef={actionRef}
                rowKey="uuid"
                columns={CurrentData.columns}
                cardBordered
                params={CurrentData.params}
                request={async (params = {}) => {
                    if (DefaultData) {
                        return { data: DefaultData, success: true }
                    } else {
                        delete params.current
                        if (CurrentData?.additional)
                            params[CurrentData.additional.key] = data[CurrentData.additional.value]
                        params.pageIndex = paramsData || 1
                        return request(CurrentData.get, { params, headers }).then(data => {
                            if (data.list) return { data: data.list, total: data.total, success: true }
                            else return { data, success: true }
                        })
                    }
                }}
                search={
                    CurrentData.hideInSearch
                        ? false
                        : {
                              className: 'searchClassName',
                              labelWidth: 'auto',
                              optionRender: (searchConfig, formProps, dom) => [
                                  ...dom.reverse(),
                                  CurrentData.exportExcel && (
                                      <Button key="out" onClick={() => {}}>
                                          导出EXCEL
                                      </Button>
                                  ),
                              ],
                          }
                }
                pagination={{ pageSize: CurrentData.pageSize, showSizeChanger: true, onChange: e => setParamsData(e) }}
                dateFormatter="string"
                toolBarRender={e => CurrentData.options}
                scroll={Scroll}
            />
        </div>
    )
}
